package fr.jade.fraclite.orb.factory;

import java.util.HashMap;
import java.util.Map;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Type;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.api.ComponentState;
import fr.jade.fraclite.factory.BasicGenericFactory;
import fr.jade.fraclite.util.FracLite;

public class FHAGenericFactory extends BasicGenericFactory {

  @ComponentState
  protected static long cnt = 10;
  
  @SuppressWarnings("unchecked")
  @Override
  public Component newFcInstance(final Type type, final Object controllerDesc, final Object contentDesc) throws InstantiationException {
    Map<String,Object> ctrlDesc;
    if(controllerDesc instanceof Map){
      ctrlDesc = (Map)controllerDesc;
    }else{
      ctrlDesc = new HashMap<String,Object>();
      ctrlDesc.put("controllerDescLabel", controllerDesc);
      ctrlDesc.put("componentId", cnt++);
    }
    Component cmp = super.newFcInstance(type, ctrlDesc, contentDesc);
    try {
      FracLite.getAttributeController(cmp).setAttribute("ha", "native");
    } catch (Exception e) {
      throw new InstantiationException("Can't set ha attribute: "+e.toString());
    }
    return cmp;
  }

}
